package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"path/filepath"
	"time"
)

type Test struct {
	Mysql mysql `yaml:"mysql"`
	Cache Cache `yaml:"cache"`
}

type mysql struct {
	User       string        `yaml:"user"`
	Host       string        `yaml:"host"`
	Password   string        `yaml:"password"`
	Port       string        `yaml:"port"`
	Name       string        `yaml:"name"`
	Timeout    time.Duration `yaml:"timeout"`
	StringTest string        `yaml:"stringTest"`
	Retry      retry         `yaml:"retry"`
}

type Cache struct {
	Enable bool     `yaml:"enable"`
	List   []string `yaml:"list,flow"`
}

type retry struct {
	Max int `yaml:"max"`
}

func main() {
	//假设这个yaml定义在同级目录
	path, _ := filepath.Abs("./test.yaml")

	test := new(Test)

	yamlFie, err := ioutil.ReadFile(path)
	if err != nil {
		fmt.Printf("ReadFile err:%v\n", err)
		return
	}

	err = yaml.Unmarshal(yamlFie, test)
	if err != nil {
		fmt.Printf("Unmarshal err:%v\n", err)
		return
	}

	fmt.Printf("test%#v\n", test)
	//test&main.Test{Mysql:main.mysql{User:"root", Host:"10.11.22.33", Password:"Tech2501", Port:"3306", Name:"cwi", Timeout:1, StringTest:"a,b,c", Retry:main.retry{Max:3}}, Cache:main.Cache{Enable:false, List:[]string{"redis", "mongoDB"}}}
}
